//
//  ChooseCityCase.m
//  定制公交
//
//  Created by thinkjoy on 16/4/13.
//  Copyright © 2016年 thinkjoy. All rights reserved.
//

#import "ChooseCityCase.h"
//#import "TJDefine.pch"
#import "CitySourceTool.h"

//#import "UIColor+Hex.h"

#pragma mark - 日志开关
#ifdef DEBUG
#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define DLog(...)
#endif

//城市的plist文件
#define RSDIVISION      @"Division"
#define RSDIVISIONNAME  @"DivisionName"
#define RSDIVISIONSUB   @"DivisionSub"

#define kWinH [[UIScreen mainScreen] bounds].size.height
#define kWinW [[UIScreen mainScreen] bounds].size.width

//宽高比系数
#define WFACTOR (kWinW/320)
#define HFACTOR (kWinH/480)

@interface ChooseCityCase ()

@property(nonatomic,strong) UIView          * pickerViewCase;
@property(nonatomic,strong) UIButton        *bgButton;
@property(nonatomic,strong)UIPickerView     *pickerView;



@property(nonatomic,strong) NSDictionary    * districtDic;//plist文件对应的数组

//省份    城市  数组
@property (nonatomic, strong) NSArray *ProvincesArray;
@property (nonatomic, strong) NSArray *CitysArray;


@property(nonatomic,assign) NSInteger provinceRow;
@property(nonatomic,assign) NSInteger cityRow;
@property (nonatomic, copy) NSString    * provinceStr;
@property (nonatomic, copy) NSString    * cityStr;

@property (nonatomic, copy) NSString    * city; //拼接好的城市数据


//@property   (nonatomic,strong)  NSArray * justProvincesAry;
//@property   (nonatomic,strong)  NSArray * justCitysAry;

@end

@implementation ChooseCityCase


- (id)initWithProvinceRow:(NSInteger)provinceRow withCityRow:(NSInteger)cityRow
{
    self    =   [super init];
    if (self) {
        
        self.frame  =   CGRectMake(0, 0, kWinW, kWinH);
        [[[UIApplication sharedApplication] keyWindow] addSubview:self];
        //半透明背景按钮
        _bgButton = [[UIButton alloc]init];
        [self addSubview:_bgButton];
        [_bgButton addTarget:self action:@selector(dismissDatePicker:) forControlEvents:UIControlEventTouchUpInside];
        _bgButton.tag   =   100;
        _bgButton.backgroundColor = [UIColor blackColor];
        _bgButton.alpha = 0.1;
        _bgButton.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
        
        _pickerViewCase =   [[UIView alloc]initWithFrame:CGRectMake(30*WFACTOR, 90*WFACTOR, 260*WFACTOR, 160*WFACTOR)];
        [self    addSubview:_pickerViewCase];
        _pickerViewCase.backgroundColor =   [UIColor whiteColor];
        
        UILabel * titleLabel    =   [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.pickerViewCase.frame.size.width, self.pickerViewCase.frame.size.height*7.0/43.0)];
        titleLabel.backgroundColor  =   [UIColor whiteColor];
        titleLabel.text =   @"请选择城市";
        titleLabel.textAlignment    =   NSTextAlignmentCenter;
        titleLabel.textColor    =   [UIColor lightGrayColor];
        [self.pickerViewCase    addSubview:titleLabel];
        
        UIButton    * commitButt    =   [UIButton buttonWithType:UIButtonTypeCustom];
        commitButt.frame    =   CGRectMake(0, self.pickerViewCase.frame.size.height*(1.0-8.0/43.0), self.pickerViewCase.frame.size.width, self.pickerViewCase.frame.size.height*8.0/43.0);
        [commitButt  setBackgroundColor:[UIColor greenColor]];
        [commitButt  setTitle:@"确定" forState:UIControlStateNormal];
        [commitButt  setTintColor:[UIColor whiteColor]];
        commitButt.tag  =   200;
        [self.pickerViewCase    addSubview:commitButt];
        [commitButt  addTarget:self action:@selector(dismissDatePicker:) forControlEvents:UIControlEventTouchUpInside];
        
        
        _pickerView    =   [[UIPickerView alloc]initWithFrame:CGRectMake(0, titleLabel.frame.size.height, self.pickerViewCase.frame.size.width, self.pickerViewCase.frame.size.height*28.0/43)];
        
        //通过plist文件取出一个大字典
        self.districtDic = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"districtMGE" ofType:@"plist"]];
        
        //从字典中取出省级列表数组
        self.ProvincesArray   =   [_districtDic objectForKey:@"Division"];
        
        self.pickerView.delegate    =   self;
        self.pickerView.dataSource  =   self;
        
        //        self.provinceRow    =   18;
        //        self.cityRow        =   0;
        
        [_pickerView selectRow:provinceRow+self.ProvincesArray.count*2 inComponent:0 animated:YES];
        [_pickerView selectRow:cityRow+self.CitysArray.count inComponent:1 animated:YES];
        self.provinceRow    =   provinceRow;
        self.cityRow        =   cityRow;
        self.provinceStr    =   [self.ProvincesArray[self.provinceRow] objectForKey:RSDIVISIONNAME];
        self.cityStr        =   [[self.ProvincesArray[self.provinceRow]objectForKey:RSDIVISIONSUB][self.cityRow]objectForKey:RSDIVISIONNAME];
        
        [self.pickerViewCase    addSubview:self.pickerView];
        [self.pickerView reloadComponent:1];
        
    }
    return self;
}

#pragma mark UIPickerViewDataSource

//返回显示的列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}
//显示每组键(列数)的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
#ifdef DEBUG
         NSLog(@"component == 0========%ld",(long)self.ProvincesArray.count);
#endif
        return self.ProvincesArray.count*5;
    }else {
        if ([pickerView selectedRowInComponent:0]   ==  -1) {
            return 0;
        }else{
#ifdef DEBUG
               NSLog(@"显示每组键(列数)的行数%ld",(long)[_pickerView selectedRowInComponent:0]%self.ProvincesArray.count);
#endif
            NSDictionary    * dic   =   self.ProvincesArray[[_pickerView selectedRowInComponent:0]%self.ProvincesArray.count];
            self.CitysArray =   [dic objectForKey:@"DivisionSub"];

            return self.CitysArray.count*10;
        }
    }
}

#pragma mark UIPickerViewDelegate
//设置列高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    if (component ==0) {
        return pickerView.frame.size.height/3.0;//第一组键(列)的高度
    }else{
        return pickerView.frame.size.height/3.0;//第二组键(列)的高度
    }
}

//返回每列的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    if (component ==0) {
        return pickerView.frame.size.width/2.0;//第一组键(列)的宽度
    }else{
        return pickerView.frame.size.width/2.0;//第二组键(列)的宽度
    }
}

//被选中的第几列 的 第几行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if(component ==0){
        //刷新对应列
        [pickerView reloadComponent:1];
        
        //当选择第一列时 第二列切到第一行
        [pickerView selectRow:self.CitysArray.count*5 inComponent:1 animated:YES];
#ifdef DEBUG
        NSLog(@"0列%ld行",(long)row);
#endif
//        self.provinceRow    =   row;
        self.cityRow    =   0;
        
        NSInteger   max =   self.ProvincesArray.count;
        if (row<2*max||row>3*max) {
            [pickerView selectRow:row%max+2*max inComponent:component animated:NO];
        }
        
        
        [self    getSelectDistrictName];
        
        
#ifdef DEBUG
        NSLog(@"滑动了0列");
#endif

    }else{
//        [pickerView reloadComponent:1];
//        self.cityRow    =   row;
        NSInteger   max =   self.CitysArray.count;
        if (row<4*max||row>6*max) {
            [pickerView selectRow:row%max+5*max inComponent:component animated:NO];
        }
        
#ifdef DEBUG
        NSLog(@"1列%ld行",(long)row);
#endif
        [self    getSelectDistrictName];
    }
}

//给行赋值
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component ==0) {
        NSDictionary    * dic   =   self.ProvincesArray[row%self.ProvincesArray.count];
        return [dic objectForKey:@"DivisionName"];//返回省的名称
    }else{
        //下面俩行代码是获得plist文件中城市的名称
        NSDictionary    * dic   =   self.CitysArray[row%self.CitysArray.count];
        return [dic objectForKey:@"DivisionName"];
    }
}

//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
//{
//    [pickerView.subviews[1] setBackgroundColor:[UIColor blueColor]];
//    [pickerView.subviews[2] setBackgroundColor:[UIColor blueColor]];
//   
//    return nil;
//}

//得到城市名,拼接到一起
- (void)getSelectDistrictName
{
    NSDictionary *ProvinceDict = self.ProvincesArray[[self.pickerView selectedRowInComponent:0]%self.ProvincesArray.count];
    
    _provinceRow    =   [_pickerView selectedRowInComponent:0]%self.ProvincesArray.count;
    
    DLog(@"++++%ld",(long)[_pickerView selectedRowInComponent:0]%self.ProvincesArray.count);
    
    self.provinceStr = [ProvinceDict objectForKey:@"DivisionName"];
    
    //*****
    
//    NSArray *array = [ProvinceDict objectForKey:@"DivisionSub"];
    
    NSDictionary *CityDict = [[ProvinceDict objectForKey:@"DivisionSub"] objectAtIndex:[_pickerView selectedRowInComponent:1]%self.CitysArray.count];
    _cityRow    =   [_pickerView selectedRowInComponent:1]%self.CitysArray.count;
    self.cityStr = [CityDict objectForKey:@"DivisionName"];

    NSString    * str = [NSString stringWithFormat:@"%@%@",self.provinceStr,self.cityStr];
    DLog(@"%ld-%@:%ld-%@",(long)
          _provinceRow,_provinceStr,(long)_cityRow,_cityStr);
    
    self.city   =   str;
}

//- (void)clickCommitButt:(UIButton *)butt
//{
//    DLog(@"确定选择城市");
////    NSLog(@"确定选择城市");
//    [self.delegate   getChooseCity:(butt.tag==100)?@"请选择城市":self.city];
//    [self    dismissDatePicker];
//}

//消失
- (void)dismissDatePicker:(UIButton *)butt
{
    if ([self respondsToSelector:@selector(get:ProvinceRow:Province:CityRow:City:)] ) {
        [self.delegate  get:self ProvinceRow:self.provinceRow Province:self.provinceStr CityRow:self.cityRow City:self.cityStr];
    }
    
    if (self.chooseCityViewBlock) {
        NSDictionary    * dic   =   @{@"ProvinceRow":@(self.provinceRow),
                                      @"Province":self.provinceStr,
                                      @"CityRow":@(self.cityRow),
                                      @"City":self.cityStr};
        self.chooseCityViewBlock(dic);
    }
    
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.2 animations:^{
        weakSelf.pickerViewCase.frame = CGRectMake(30*WFACTOR, kWinH, 260*WFACTOR, 160*WFACTOR);
        weakSelf.bgButton.alpha = 0.0;
        
    } completion:^(BOOL finished) {
        [weakSelf.pickerViewCase removeFromSuperview];
        [weakSelf.bgButton removeFromSuperview];
        [weakSelf removeFromSuperview];
        
    }];
}

@end
